package com.bussiness.system.controller;

import com.bussiness.config.annotions.MyRoleAuth;
import com.bussiness.system.dao.MyAuthDao;
import com.bussiness.system.dao.MyRoleAuthDao;
import com.bussiness.system.dao.MyRoleDao;
import com.bussiness.system.model.MyAuthEntity;
import com.bussiness.system.model.MyRoleEntity;
import com.bussiness.system.service.RoleAuthService;
import com.bussiness.system.dto.MyAuthDto;
import com.bussiness.system.dto.MyRoleAuthDto;
import com.bussiness.system.dto.MyRoleDto;
import com.common.response.ResponseBean;
import com.common.response.impl.BaseResponseImpl;
import com.common.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zyj
 * @description
 * @date 2019/9/23 19:07
 **/
@RestController
@RequestMapping("/roleAuth")
@MyRoleAuth
@Api(tags = {"角色资源"})
public class RoleAuthController extends BaseResponseImpl {

    Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    public MyAuthDao myAuthDao;
    @Autowired
    public MyRoleDao myRoleDao;
    @Autowired
    public RoleAuthService roleAuthService;
    @Autowired
    private MyRoleAuthDao myRoleAuthDao;

    @ApiOperation(value = "添加资源")
    @RequestMapping(value = "/addAuth" ,method = RequestMethod.POST)
    public ResponseBean addAuth(@RequestBody MyAuthDto MyAuthDto){
        try {
            MyAuthEntity MyAuthEntity = new MyAuthEntity();
            BeanUtils.copyProperties(MyAuthDto, MyAuthEntity);
            myAuthDao.insertSelective(MyAuthEntity);
            return success();
        }catch (Exception e){
            log.error("添加资源失败",e);
            return errorSave();
        }
    }


    @ApiOperation(value = "修改资源")
    @RequestMapping(value = "/updateAuth", method = RequestMethod.POST)
    public ResponseBean updateAuth(@RequestBody MyAuthDto MyAuthDto){
        try {
            MyAuthEntity myAuthEntity = myAuthDao.selectByPrimaryKey(MyAuthDto.getId());
            BeanUtils.copyProperties(MyAuthDto, myAuthEntity);
            myAuthDao.updateByPrimaryKeySelective(myAuthEntity);
            return success();
        }catch (Exception e){
            log.error("修改资源失败",e);
            return errorUpdate();
        }
    }

    @ApiOperation(value = "添加角色")
    @RequestMapping(value =  "/addRole" ,method = RequestMethod.PUT)
    public ResponseBean addRole(@RequestBody MyRoleDto myRoleDto){
        try {
            MyRoleEntity MyRoleEntity = new MyRoleEntity();
            BeanUtils.copyProperties(myRoleDto, MyRoleEntity);
            myRoleDao.insertSelective(MyRoleEntity);
            return success();
        }catch (Exception e){
            log.error("添加角色失败",e);
            return errorSave();
        }

    }

    @ApiOperation(value = "修改角色")
    @RequestMapping(value = "/updateRole" ,method = RequestMethod.PUT)
    public ResponseBean updateRole(@RequestBody MyRoleDto myRoleDto){
        try {
            MyRoleEntity myRoleEntity = myRoleDao.selectByPrimaryKey(Long.parseLong(myRoleDto.getId()));
            BeanUtils.copyProperties(myRoleDto, myRoleEntity);
            myRoleDao.updateByPrimaryKeySelective(myRoleEntity);
            return success();
        }catch (Exception e){
            log.error("修改角色失败",e);
            return errorUpdate();
        }

    }

    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "/deleteRole" ,method = RequestMethod.DELETE)
    public ResponseBean deleteRole(@RequestParam Long roleId){
        try {
            myRoleDao.deleteByPrimaryKey(roleId);
            return success();
        }catch (Exception e){
            log.error("删除角色失败",e);
            return errorUpdate();
        }

    }

    @ApiOperation(value = "获取角色列表")
    @GetMapping(value = "/getRoleList" , produces = "application/json")
    public ResponseBean getRoleList(Integer pageSize, Integer pageNumber, String name ){
        try {
            return successData(roleAuthService.roleMyPage(pageSize, pageNumber, name));
        }catch (Exception e){
            log.error("获取角色列表失败",e);
            return error();
        }
    }

    @ApiOperation(value = "修改角色权限(根据角色修改)")
    @PutMapping(value = "/updateRoleAuth" )
    public ResponseBean updateRoleAuth(@RequestBody List<MyRoleAuthDto> MyRoleAuthDtos){
        try {
            roleAuthService.updateRoleAuth(MyRoleAuthDtos);
            return success();
        }catch (Exception e){
            log.error("修改角色权限(根据角色修改)失败",e);
            return errorUpdate();
        }
    }

    @ApiOperation(value = "获取角色权限列表")
    @GetMapping(value = "/getRoleAuthList" , produces = "application/json")
    public ResponseBean getRoleAuthList(Long roleId){
        try {
            return successData(myRoleAuthDao.selectByRoleId(roleId));
        }catch (Exception e){
            log.error("获取角色权限列表失败",e);
            return error();
        }
    }

    @ApiOperation(value = "获取资源树")
    @RequestMapping(value = "/getAuthTree", method = RequestMethod.GET)
    public  List<TreeVo> getAuthTree(@RequestParam String id){

        try {
            if(id==null||id.equals("")){
                id = "0";
            }
            List<TreeVo> childrenTree = roleAuthService.getChildrenTree(id);
            return childrenTree;
        } catch (Exception e){
            log.error("获取资源树失败", e);
            return null;
        }
    }

    /**
     * 获取资源
     * @throws Exception
     */
    @RequestMapping(value = "/getResourceList", method = RequestMethod.GET)
    public ResponseBean getResourceList(Long roleId){
        try {
            List<TreeVo> resource = roleAuthService.getResource(roleId);
            return successData(resource);
        } catch (Exception e){
            log.error("根据id获取资源失败", e);
            return error();
        }
    }

    @ApiOperation(value = "根据id获取资源")
    @RequestMapping(value = "/getAuthById", method = RequestMethod.GET)
    public  ResponseBean getAuthById(@RequestParam Long id){

        try {
            MyAuthEntity myAuthEntity =  myAuthDao.selectByPrimaryKey(id);
            return successData(myAuthEntity);
        } catch (Exception e){
            log.error("根据id获取资源失败", e);
            return error();
        }

    }

    @ApiOperation(value = "根据id删除资源")
    @RequestMapping(value = "/deleteAuthById", method = RequestMethod.DELETE)
    public  ResponseBean deleteAuthById(@RequestParam Long id){

        try {
            myAuthDao.deleteByPrimaryKey(id);
            return success();
        } catch (Exception e){
            log.error("删除资源", e);
            return error();
        }

    }

    @ApiOperation(value = "根据id获取按钮资源")
    @RequestMapping(value = "/getResBtn", method = RequestMethod.GET)
    public ResponseBean getResBtn(Long parentId){
        try {
            List<MyAuthEntity> list = myAuthDao.getAuthBtnByParentId(parentId);
            return successData(list);
        } catch (Exception e){
            log.error("获取资源失败", e);
            return error();
        }
    }

}
